{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Prompt versioning\n",
        "\n",
        "<a target=\"_blank\" href=\"https://colab.research.google.com/github/masci/banks/blob/main/cookbook/Prompt_Versioning.ipynb\">\n",
        "  <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
        "</a>\n",
        "\n",
        "A prompt is almost never set in stone. It can change over time as we find nuances in the language that improve \n",
        "performance, it can change as the model being used gets update, it almost certainly changes when the same prompt is \n",
        "used against different models.\n",
        "\n",
        "In all these situations, being able to attach a version to a prompt can greatly help to keep things tidy, organized \n",
        "and ultimately save time. Let's see how to do this with Banks."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "LTucJvi7Xor_"
      },
      "outputs": [],
      "source": [
        "!pip install banks"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We'll store our templates in a local folder called `templates`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "iCSH4kOaczt5"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "\n",
        "os.mkdir(\"templates\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "We now write two versions of the same prompt, optimized for different LLMs. The two prompts will share the same\n",
        "`name` but will have different `version`s."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "UaSSFjnUXzMD"
      },
      "outputs": [],
      "source": [
        "from pathlib import Path\n",
        "\n",
        "from banks import Prompt\n",
        "from banks.registries import DirectoryPromptRegistry\n",
        "\n",
        "# Tell the registry where prompt texts are stored\n",
        "registry = DirectoryPromptRegistry(Path(\".\") / \"templates\")\n",
        "\n",
        "# Write two versions of the same prompt, optimized for different LLMs\n",
        "blog_prompt_gpt = Prompt(\"Write a 500-word blog post on {{ topic }}.\\n\\nBlog post:\", name=\"blog_prompt\", version=\"gpt-3.5-turbo\")\n",
        "# Llama usually benefits a lot from in-context learning, let's add examples\n",
        "blog_prompt_llama3 = Prompt(\n",
        "    \"Write a blog post abot the topic {{ topic }}. Do not write more than 500 words\"\n",
        "    \"Examples:\"\n",
        "    \"{% for example in examples %}\"\n",
        "    \"{{ example }}\"\n",
        "    \"{% endfor %}\"\n",
        "    \"\\n\\nBlog post:\", name=\"blog_prompt\", version=\"ollama_llama3.1:8b\"\n",
        ")\n",
        "\n",
        "# Store the two prompts\n",
        "registry.set(prompt=blog_prompt_gpt)\n",
        "registry.set(prompt=blog_prompt_llama3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "When we need a prompt, we can now ask the registry for the version of choice."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "IyVpMFN7dAhW"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "\n",
        "from litellm import completion\n",
        "\n",
        "\n",
        "## set ENV variables\n",
        "os.environ[\"OPENAI_API_KEY\"] = \"your-api-key\"\n",
        "\n",
        "\n",
        "response = completion(\n",
        "    model=\"gpt-3.5-turbo\",\n",
        "    messages=registry.get(name=\"blog_prompt\", version=\"gpt-3.5-turbo\").chat_messages(),\n",
        ")"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
